const nodemailer = require('nodemailer')
const config = require('../config')

const sendMail = (options) => {

  const transporter = nodemailer.createTransport({
    host: 'smtp.163.com',
    secureConnection: true, // use SSL 
    port: 465,
    secure: true, // secure:true for port 465, secure:false for port 587
    auth: {
      user: options.user || config.mailer.user,
      pass: options.pass || config.mailer.pass //授权码,在QQ邮箱生成
    }
   })
   
   const mailOptions = {
     from: options.from || config.mailer.from, // 发送者
     to: options.to, // 接受者,逗号隔开
     subject: options.subject, // 标题
     text: options.text || ' ', // 文本 文本和html 2选1
     html: options.html || '', // html
     // attachments: [ // 附件
     //   {
     //     filename: 'package.json',
     //     path: './package.json'
     //   }
     // ]
   }
   console.log(mailOptions)
   return new Promise((resolve, reject) => {
    transporter.sendMail(mailOptions, (err, info) => {
      if (err) return reject(err)
      resolve(info)
     })
   })
}

// sendMail({
//   to: [
//     '956164483@qq.com'
//   ],
//   subject: '标题',
//   html: `
//   <a href="http://www.baidu.com">最新地址</a>
//   `
// }).then(info => {
//   console.log(info)
// }).catch(err => {
//   console.log(err)
// })

module.exports = sendMail